#include "Thread.h"
using namespace cvs;

// static variable definition
AtomicInt32 Thread::numCreated_;

Thread::Thread(const threadFun& f, const std::string name/*=std::string()*/) 
	: func_(f)
	, name_(name)
	, started_(false)
	, joined_(false)
{
	numCreated_.increment();
}

Thread::~Thread() {
	if (started_ && !joined_) {
		printf("tid:%d ~Thread()\n", threadId_.get_id());
		threadId_.detach();
	}
}

void Thread::start(void) {
	assert(!started_);

	threadId_ = boost::thread(func_);
	started_ = true;
}

void Thread::join(void) {
	assert(started_);
	assert(!joined_);
	joined_ = true;

	threadId_.join();
}